package com.yunquan.ohana.utils

import android.content.Context
import android.content.res.Configuration
import android.view.ContextThemeWrapper
import java.util.Locale

object LocaleHelper {
    /**
     * Wrap the given Activity context with a locale-aware ContextThemeWrapper
     * so that UI components (e.g., Dialogs) use the selected language while
     * still retaining a valid window token from the Activity.
     */
    fun wrap(context: Context, locale: Locale): Context {
        val newConfig = Configuration(context.resources.configuration)
        newConfig.setLocale(locale)
        // Use a ContextThemeWrapper to keep the Activity as base context (for window token)
        // while applying the locale via override configuration.
        return ContextThemeWrapper(context, 0).apply {
            applyOverrideConfiguration(newConfig)
        }
    }
}